package com.thinkin.util;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.thinkin.fondation.po.SkuType;
import com.thinkin.jdbc.util.BaseDaoUtil;
import com.thinkin.jdbc.util.DataUtil;

public class CommonServer {
	Logger logger = LoggerFactory.getLogger(CommonServer.class);
	BaseDaoUtil baseDaoUtil = null;

	public CommonServer() {
		baseDaoUtil = new BaseDaoUtil();
	}

	public String doSave(String sql, Object[] param, Boolean blean)
			throws Exception {
		int result = 0;
		try {
			baseDaoUtil.beginTransaction();
			result = baseDaoUtil.exeSql(sql, param);
			baseDaoUtil.commitTransaction();
			if (blean) {
				baseDaoUtil.closeAll();
			}
		} catch (Exception e) {
			throw e;
		}
		return result + "";
	}

	public String doEdit(String sql, Object[] param, Boolean blean)
			throws Exception {
		// TODO Auto-generated method stub
		int result = 0;
		try {
			baseDaoUtil.beginTransaction();
			result = baseDaoUtil.exeSql(sql, param);
			baseDaoUtil.commitTransaction();
			if (blean) {
				baseDaoUtil.closeAll();
			}
		} catch (Exception e) {
			throw e;
		}
		return result + "";
	}

	public String doDelete(String sql, Object[] param, Boolean blean)
			throws Exception {
		// TODO Auto-generated method stub
		int result = 0;
		try {
			baseDaoUtil.beginTransaction();
			result = baseDaoUtil.exeSql(sql, param);
			baseDaoUtil.commitTransaction();
			if (blean) {
				baseDaoUtil.closeAll();
			}
		} catch (Exception e) {
			throw e;
		}
		return result + "";
	}

	public String doDelete(String sql, Boolean blean) {
		// TODO Auto-generated method stub
		baseDaoUtil.beginTransaction();
		baseDaoUtil.exeSql(sql);
		baseDaoUtil.commitTransaction();
		if (blean) {
			baseDaoUtil.closeAll();
		}
		return "";
	}

	public Map<String, Object> doFindEntity(String sql, String param,
			Boolean blean) {
		Map<String, Object> map = new HashMap<String, Object>();
		baseDaoUtil.beginTransaction();
		ResultSet rs = baseDaoUtil.getRs(sql, param);
		if (rs != null) {
			try {
				while (rs.next()) {
					ResultSetMetaData rsmd = rs.getMetaData();
					int colCount = rsmd.getColumnCount();

					for (int i = 0; i < colCount; i++) {
						String key = rsmd.getColumnName(i + 1).toLowerCase();
						Object value = rs.getString(i + 1);
						map.put(key, value);

					}

				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (blean) {
			baseDaoUtil.closeAll();
		}
		return map;
	}

	public List<SkuType> findQuery(String sql, Object[] param, Boolean blean,
			Class css) {
		// TODO Auto-generated method stub
		baseDaoUtil.beginTransaction();
		ResultSet result = baseDaoUtil.getRs(sql, param);
		Collection<SkuType> collection = new ArrayList<SkuType>();
		collection = DataUtil.getDatas(collection, result, css);
		if (blean) {
			baseDaoUtil.closeAll();
		}
		return (List<SkuType>) collection;
	}

	public List<Map<String, Object>> doQuery(String sql, Boolean blean) {
		// TODO Auto-generated method stub
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		try {
			baseDaoUtil.beginTransaction();
			ResultSet rs = baseDaoUtil.getRs(sql);

			if (rs != null) {
				try {
					while (rs.next()) {
						ResultSetMetaData rsmd = rs.getMetaData();
						int colCount = rsmd.getColumnCount();
						Map<String, Object> map = new HashMap<String, Object>();
						for (int i = 0; i < colCount; i++) {
							String key = rsmd.getColumnName(i + 1)
									.toLowerCase();
							Object value = rs.getString(i + 1);
							map.put(key, value);

						}
						list.add(map);
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		} catch (Exception e) {
			// TODO 获取执行sql时异常信息，可返回
		}
		if (blean) {
			baseDaoUtil.closeAll();
		}
		return list;
	}

	public List<Map<String, Object>> doQuery(String sql, Object[] param,
			Boolean blean) {
		// TODO Auto-generated method stub
		baseDaoUtil.beginTransaction();
		ResultSet rs = baseDaoUtil.getRs(sql, param);
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		if (rs != null) {
			try {
				while (rs.next()) {
					ResultSetMetaData rsmd = rs.getMetaData();
					int colCount = rsmd.getColumnCount();
					Map<String, Object> map = new HashMap<String, Object>();
					for (int i = 0; i < colCount; i++) {
						String key = rsmd.getColumnName(i + 1).toLowerCase();
						Object value = rs.getString(i + 1);
						map.put(key, value);

					}
					list.add(map);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		if (blean) {
			baseDaoUtil.closeAll();
		}
		return list;
	}

	/**
	 * 获取执行多条sql时的结果集
	 * 
	 * @param cmdText
	 *            拼接好的过滤条件，以--~进行分割
	 * @param blean
	 *            是否关闭链接
	 * @return 返回结果集的字符串对象
	 */
	public String doQueryBatch(String cmdTexts, Boolean blean) {

		// 分割sql字符串
		String[] sCommandTexts = cmdTexts.split("---~");
		// 定义返回的对象
		String Result = "{";
		baseDaoUtil.beginTransaction();
		try {
			for (int i = 0; i < sCommandTexts.length; i = i + 2) {
				List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
				String cmdText = sCommandTexts[i + 1].toString();
				if (cmdText == null || cmdText.length() <= 0) {
					continue;
				}
				ResultSet rs = baseDaoUtil.getRs(cmdText);
				if (rs != null) {
					while (rs.next()) {
						ResultSetMetaData rsmd = rs.getMetaData();
						int colCount = rsmd.getColumnCount();
						Map<String, Object> map = new HashMap<String, Object>();
						for (int j = 0; j < colCount; j++) {
							String key = rsmd.getColumnName(j + 1)
									.toLowerCase();
							Object value = rs.getString(j + 1);
							map.put(key, value);
						}
						list.add(map);
					}
				}
				if (Result.length() > 1) {
					Result = Result + ",";
				}
				Result = Result + "\"" + sCommandTexts[i].toString() + "\":";
				Result = Result + JsonHelper.toJSONString(list);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			Result = "{\"Error\":'" + e.getMessage() + "'}";
		}
		if (blean) {
			baseDaoUtil.closeAll();
		}
		if (Result.indexOf("Error") == -1) {
			Result = Result + "}";
		}
		return Result.replaceAll("[\\n\\r\\t]", "");
	}

}
